package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.domain.flowerUser.cqrs.FlowerUserPageQuery;
import com.starlight.flower_server.infrastructure.entity.FlowerUser;
import com.starlight.flower_server.infrastructure.mapper.FlowerUserMapper;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import com.starlight.flower_server.infrastructure.util.PageUtils;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author yfy2001
 * @date 2023/4/17 18:49
 */
@Repository
public class FlowerUserRepositoryImpl implements FlowerUserRepository {

    @Resource
    private FlowerUserMapper flowerUserMapper;

    @Override
    public Integer insert(FlowerUser flowerUser) {
        flowerUser.setCreator("SYSTEM");
        flowerUser.setModifier("SYSTEM");
        flowerUser.setCreated(new Date());
        flowerUser.setModified(new Date());
        flowerUser.setIsDeleted(0);
        return flowerUserMapper.insert(flowerUser);
    }

    @Override
    public FlowerUser queryOneByUserDetail(FlowerUser flowerUserDetail) {
        return flowerUserMapper.selectOne(new LambdaQueryWrapper<FlowerUser>()
                .eq(Objects.nonNull(flowerUserDetail.getUsername()),FlowerUser::getUsername, flowerUserDetail.getUsername())
                .or()
                .eq(Objects.nonNull(flowerUserDetail.getEmail()),FlowerUser::getEmail, flowerUserDetail.getEmail()));
    }

    @Override
    public FlowerUser queryDetail(Long id) {
        return flowerUserMapper.selectById(id);
    }

    @Override
    public Integer logicDelete(Long id) {
        return flowerUserMapper.deleteById(id);
    }

    @Override
    public Integer modify(FlowerUser flowerUser) {
        String modifier= CurrentUserInfoUtils.get("username");
        flowerUser.setModifier(modifier);
        flowerUser.setModified(new Date());
        return flowerUserMapper.updateById(flowerUser);
    }

    @Override
    public boolean exists(FlowerUser flowerUser) {
        return flowerUserMapper.exists(new LambdaQueryWrapper<FlowerUser>()
                .ne(Objects.nonNull(flowerUser.getId()),FlowerUser::getId,flowerUser.getId())
                .eq(Objects.nonNull(flowerUser.getUsername()),FlowerUser::getUsername,flowerUser.getUsername())
                .or()
                .ne(Objects.nonNull(flowerUser.getId()),FlowerUser::getId,flowerUser.getId())
                .eq(Objects.nonNull(flowerUser.getEmail()),FlowerUser::getEmail,flowerUser.getEmail()));

    }

    @Override
    public Map<Long, FlowerUser> queryMap(List<Long> flowerUserIdList){
        if (Objects.isNull(flowerUserIdList) || flowerUserIdList.isEmpty()){
            return Collections.emptyMap();
        }
        QueryWrapper<FlowerUser> query = new QueryWrapper<>();
        query.lambda().in(FlowerUser::getId,flowerUserIdList);
        List<FlowerUser> list = flowerUserMapper.selectList(query);
        return list.stream().collect(Collectors.toMap(FlowerUser::getId, v->v,(v1, v2)->v2));
    }

    @Override
    public IPage<FlowerUser> queryUserPage(FlowerUserPageQuery query) {
        QueryWrapper<FlowerUser> queryWrapper= PageUtils.createOrderQuery(query);
        queryWrapper.lambda()
                .isNotNull(FlowerUser::getId)
                .eq(Objects.nonNull(query.getId()), FlowerUser::getId,query.getId())
                .like(Objects.nonNull(query.getUsername()),FlowerUser::getUsername,query.getUsername())
                .like(Objects.nonNull(query.getNickname()),FlowerUser::getNickname,query.getNickname());
        return flowerUserMapper.selectPage(PageUtils.startPage(query),queryWrapper);
    }

    @Override
    public List<FlowerUser> queryAll() {
        return flowerUserMapper.selectList(new LambdaQueryWrapper<FlowerUser>()
                .isNotNull(FlowerUser::getId));
    }

    @Override
    public Long queryTotalCount() {
        return flowerUserMapper.selectCount(new LambdaQueryWrapper<FlowerUser>()
                .isNotNull(FlowerUser::getId));
    }
}
